<?php
if (!defined('ROOT')) exit('SYTbHelper');
//封禁相关
class ban_model extends base {
	private $banlist;
	private $tmpbanlist;
	static private $_instance=NULL;
	static public function getInstance () {
		if (!self::$_instance) self::$_instance=new self;
		return self::$_instance;
	}
	public function __construct () {
		parent::init();
		$_ENV['_now']='ban';
		$this->banlist=$this->db('get','banlist');
		$this->ban_inittmplist();
	}
	private function ban_inittmplist ($renew=FALSE) { //建立临时封禁表
		if ($renew) { //强制更新
			$this->tmpbanlist=$this->banlist;
			$this->db('set','tmpbanlist',$this->tmpbanlist);
			$this->setval('lastban',date('Ymd'));
		} else {
			$last=$this->getval('lastban');
			if ($last!=date('Ymd')) {
				$this->tmpbanlist=$this->banlist;
				$this->db('set','tmpbanlist',$this->tmpbanlist);
				$this->setval('lastban',date('Ymd'));
			} else {
				$this->tmpbanlist=$this->db('get','tmpbanlist');
			}
		}
	}
	private function ban_gettmpnum () {
		return count($this->tmpbanlist);
	}
	private function ban_getonetmp () {
		return each($this->tmpbanlist);
	}
	private function ban_removetmp ($id) {
		unset($this->tmpbanlist[$id]);
		$this->db('set','tmpbanlist',$this->tmpbanlist);
	}
	private function ban_add ($data) { //$id,$day,$fid,$tieba
		$this->banlist[]=$data;
		$this->db('set','banlist',$this->banlist);
	}
	private function ban_remove ($id) {
		unset($this->banlist[$id]);
		$this->db('set','banlist',$this->banlist);
	}
	//封禁一个
	public function ban_one ($id,$fid,$day,$tieba='',$inid) {
		//Web版的接口有奇怪的问题，所以使用Wap版的接口
		/*$post=array(
			'user_name[]'=>$id,
			'day'=>$day,
			'fid'=>$fid,
			'tbs'=>get_tbs(),
			'reason'=>'大量散布广告，发表交易帖，屡教不改，给予封禁处罚。'
		);
		$re=jd(curl_get($this->mkurl('pmc/blockid'),array('post'=>$post)));*/
		$re=jd(curl_get($this->mkurl('mo/q/m?tn=bdFIL&ntn=banid&day='.$day.'&un='.$id.'&z='.time().'&tbs='.$this->get_tbs().'&word='.$tieba.'&fid='.$fid.'&reason=大量散布广告，发表交易帖，屡教不改，给予封禁处罚。')));
		if (!$re) {
			echo '<li class="list-group-item">JSON解析错误</li>';
			return FALSE;
		} elseif ($re['errno']==0) {
			echo '<li class="list-group-item list-group-item-success">封禁成功！封禁账号：'.$id.'，天数：'.$day.'，所在贴吧：'.$tieba.'，fid为'.$fid.'</li>';
			$this->ban_removetmp($inid);
			return TRUE;
		} else {
			echo '<li class="list-group-item list-group-item-warning">封禁失败！返回信息：',$re['errmsg'],'，封禁账号：',$id,'，天数：',$day,'，所在贴吧：',$tieba,'，fid为',$fid,'</li>';
			return FALSE;
		}
	}
	//添加用户
	private function ban_adduser ($id,$day,$tieba) {
		$fid=$this->get_fid($tieba);
		$data=array('id'=>$id,'day'=>$day,'fid'=>$fid,'tieba'=>$tieba);
		$this->ban_add($data);
		$this->ShowTips('添加成功！贴吧fid为：'.$fid.'，被封禁用户为'.$id,'success');;
	}
	//移除用户
	private function ban_removeuser ($id) {
		$this->ban_remove($id);
		$this->ShowTips('成功删除','success');
	}
	//主函数，进行封禁
	private function ban () {
		if (!$this->is_login()) $this->DisplayError('Cookie错误');
		ob_start();
		$i=0;
		while ($row=$this->ban_getonetmp()) {
			if ($i==10) break;
			list($id,$vals)=$row;
			$this->ban_one($vals['id'],$vals['fid'],$vals['day'],$vals['tieba'],$id);
			$i++;
		}
		$num=$this->ban_gettmpnum();
		if ($num!=0) $continue='<div class="alert alert-success"><a href="index.php?m=ban&a=test" class="btn btn-success">还剩下'.$num.'个用户没有封禁，点击这里继续</a></div>';
		else $continue='<div class="alert alert-success">所有用户已封禁</div>';
		if (defined('CRON')) {
			ob_end_clean();
			echo 'CRON';
		} else {
			$info=ob_get_clean();
			include(TPL.'ban_result.html');
		}
	}
	//CRON
	public function cron () {
		$this->ban();
		$num=$this->ban_gettmpnum();
		if ($num!=0) {
			task_add('ban');
		}
	}
	//列出
	public function onlist () {
		$list='';
		foreach ($this->banlist as $key=>$row) {
			$list.='<tr><td>'.$row['tieba'].'</td><td>'.$row['id'].'</td><td>'.$row['day'].'</td><td><a href="?m=ban&a=del&id='.$key.'" class="btn btn-danger">删除</a></td></tr>';
		}
		include(TPL.'ban_list.html');
	}
	//添加
	public function onadd () {
		if (isset($_POST['submit'])) {
			$id=htmlspecialchars($_POST['id']);
			$day=intval($_POST['day']);
			$tieba=htmlspecialchars($_POST['tieba']);
			if (empty($id) || empty($day) || empty($tieba)) $this->DisplayError('请填写完整！');
			$this->ban_adduser($id,$day,$tieba);
		} else {
			include(TPL.'ban_add.html');
		}
	}
	//删除
	public function ondel () {
		$id=intval($_GET['id']);
		if (!isset($_GET['id']) || $_GET['id']=='') $this->DisplayError('参数错误！');
		$this->ban_removeuser($id);
	}
	//测试
	public function ontest () {
		$this->ban();
	}
}

?>